package com.example.chirdrenserver.Service.UserService.Impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chirdrenserver.Holder.UserHolder;
import com.example.chirdrenserver.Pojo.dto.UserUpdateDTO;
import com.example.chirdrenserver.Pojo.entity.File;
import com.example.chirdrenserver.Pojo.entity.User;
import com.example.chirdrenserver.Service.FileService;
import com.example.chirdrenserver.Service.UserService.UserService;
import com.example.chirdrenserver.exception.BaseException;
import com.example.chirdrenserver.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: Yu
 * @CreateTime: 2025-06-27
 * @Description:
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    FileService fileService;

    @Override public void updateUser(UserUpdateDTO userUpdateDTO) {
        if (BeanUtil.isEmpty(fileService.getOne(new LambdaQueryWrapper<File>().eq(File::getFileKey, userUpdateDTO.getAvatarFileKey())))) {
            throw new BaseException("文件不存在");
        }

        User user = new User();
        user.setId((Long) UserHolder.get());
        BeanUtil.copyProperties(userUpdateDTO, user);

        if (!updateById(user)) {
            throw new BaseException("更新失败");
        }
    }
}
